package com.buobao.spring.jdbc.service;

import com.buobao.spring.jdbc.dao.EmployeeDao;
import com.buobao.spring.jdbc.entity.Employee;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional
public class EmployeeService {
    @Resource
    private EmployeeDao employeeDao;

    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)  //单独配置这个方法关闭事务 并且只能做制度操作
    public Employee findById(Long eno)
    {
        return employeeDao.findById(eno);
    }

    @Transactional(propagation = Propagation.REQUIRED)   //这个注解也可以直接放在类上
    public void insertBatch() {
        for (int i=0;i<10;i++) {
            Employee employee = new Employee();
            employee.setEmpno(9999l+i);
            employee.setEname("new insert"+i);
            employee.setHiredate(new Date());
            employee.setMgr(7698l);
            employee.setSal(8000.0);
            employee.setComm(10.0);
            employee.setJob("test job");
            employee.setEname("张泽涛");
            employee.setDeptno(10l);
            employeeDao.insert(employee);
            if (i==5) {
                throw new RuntimeException("error");
            }
        }
    }
}
